package com.cilico.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.cilico.R;
import com.cilico.citylist.CityListActivity;
import com.cilico.utils.ConstantRepository;
import com.cilico.utils.Utils;

public class GuideBook extends Activity {
	LinearLayout GuideBookScreen_;
	TV tv_;
	Bitmap img_ = null;
	int gridcount; // 总页数
	int gridcount1 = 5; // 用户指引总页数
	int gridcount2 = 0; // 新增功能总页数
	int currentPageIndex_ = 1;
	boolean key = false; // true：升级  false：首次安装
	int xDown_;
	int xUp_;

	final class TV extends TextView {

		public TV(Context context) {
			super(context);
		}

		protected void onDraw(Canvas canvas) {
			if (img_ != null) {
				img_.recycle();
				img_ = null;
			}
			if (key) {// 该升级版本新功能图片

			} else {// 指引机制图片
				switch (currentPageIndex_) {
				case 1:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.feature_guide_1),
							Utils.screenWidth_, Utils.screenHeight_ - 25,true);
					break;
				case 2:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.feature_guide_2),
							Utils.screenWidth_, Utils.screenHeight_ - 25, true);
					break;
				case 3:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.feature_guide_3),
							Utils.screenWidth_, Utils.screenHeight_ - 25, true);
					break;
				case 4:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.feature_guide_4),
							Utils.screenWidth_, Utils.screenHeight_ - 25, true);
					break;
				case 5:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.feature_guide_5),
							Utils.screenWidth_, Utils.screenHeight_ - 25, true);
					break;
				default:
					img_ = Bitmap.createScaledBitmap(ConstantRepository.decodeBitmap(getResources(), R.drawable.loading),
							Utils.screenWidth_, Utils.screenHeight_ - 25, true);
					break;
				}
			}
			Paint paint_ = new Paint();
			canvas.drawBitmap(img_, 0, 0, paint_);
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		Bundle requestdata = intent.getExtras();
		key = requestdata.getBoolean("key");
		if (key) {
			gridcount = gridcount2;
		} else {
			gridcount = gridcount1;
		}
		Utils.calculateScreenSize(this);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		GuideBookScreen_ = new LinearLayout(this);
		GuideBookScreen_.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		GuideBookScreen_.setOrientation(LinearLayout.VERTICAL);

		tv_ = (TV) addTextView();
		GuideBookScreen_.addView(tv_);
		GuideBookScreen_.setBackgroundResource(R.drawable.background);// 设置背景颜色
		setContentView(GuideBookScreen_);

	}

	private View addTextView() {
		TextView tv = new TV(this);
		tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
		tv.setGravity(1);
		return tv;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 取得触摸动作类型
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			xDown_ = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			xUp_ = (int) event.getX();
			if (xDown_ - xUp_ > 60 || (Math.abs(xUp_ - xDown_) < 8 && xUp_ > Utils.screenWidth_ / 2)) {
				if (currentPageIndex_ < gridcount) {
					currentPageIndex_++;
					tv_.invalidate();
				} else if (currentPageIndex_ >= gridcount) {
					SplashScreen.gotoStart_ = true;
					finish();
					
					// 跳转至城市定位
					Intent intent = new Intent();
					intent.setClass(GuideBook.this, CityListActivity.class);
					startActivity(intent);
				}
			}
			if (xUp_ - xDown_ > 60 || (Math.abs(xUp_ - xDown_) < 8 && xUp_ <= Utils.screenWidth_ / 2)) {
				if (currentPageIndex_ > 1) {
					currentPageIndex_--;
					tv_.invalidate();
				}
			}
			break;
		default:
			return super.onTouchEvent(event);
		}
		return true;
	}

	protected void onDestroy() {
		if (img_ != null) {
			img_.recycle();
			img_ = null;
		}
		if (tv_ != null) {
			tv_ = null;
		}
		if (GuideBookScreen_ != null) {
			GuideBookScreen_ = null;
		}
		System.gc();
		super.onDestroy();
	}
}
